package response

import (
	"github.com/tal-tech/go-zero/rest/httpx"
	"karnin-zero-admin/common/errorx"

	"net/http"
)

type Body struct {
	Code       int         `json:"code"`
	Msg        string      `json:"msg"`
	Data       interface{} `json:"data"`
	ResultCode int         `json:"result_code"`
}

func Response(w http.ResponseWriter, resp interface{}, err error) {
	var body Body
	if err != nil {
		switch e := err.(type) {
		case *errorx.CodeError:
			body.Code = 0
			body.Msg = e.Msg
			body.Data = make(map[string]string)
			body.ResultCode = e.Code
		}
	} else {
		body.Code = 1
		body.Msg = "OK"
		body.Data = resp
		body.ResultCode = errorx.OKCode
	}
	httpx.OkJson(w, body)
}
